package com.belizer.demo.thread;

import java.util.Random;
import java.util.concurrent.*;

public class Demo6 {

    public static void main(String[] args) {
        Callable<Integer> callable = (() -> {
            //computing ...
            TimeUnit.SECONDS.sleep(3);

            return new Random().nextInt();
        });
        FutureTask<Integer> ft = new FutureTask<>(callable);

        //中断执行任务的线程
        boolean cancel = ft.cancel(true);
        System.out.println("interrupt task,status: " + cancel);

        ft.run();




        try {
            Integer result = ft.get();
            System.out.println("task compute completed,result: " + result);
        } catch (CancellationException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}
